<script setup>
import { ref, nextTick, onBeforeUnmount } from 'vue'
import Vrouter from "@/router"
import { MYURL } from '@/utils/express/express.js'
import { getChatList } from '@/api/chat/chat.js'
import time from '@/utils/time/time.js'
import { userStore } from '@/stores/user.js'
import { emojiDecode } from '@/utils/emoji/emoji.js'
const MyStore = userStore()
const list = ref([])
const updateChatList = () => {
    getChatList().then((res) => {
        list.value = res.data.message
        nextTick(() => {
            isEmpty.value = false
        })
    })
}
updateChatList()
let socket
nextTick(() => {
    socket = MyStore.socket
    socket.on('updateChatList', () => {
        updateChatList()
    })
})
onBeforeUnmount(() => {
    socket.removeAllListeners('updateChatList')
})
const isEmpty = ref(true)
const router = Vrouter
let imgURL = `${MYURL}/public/icon/back_black.png`
</script>

<template>
    <div v-if="isEmpty">
        <IsEmpty></IsEmpty>
    </div>
    <div v-if="!isEmpty">
        <div class="get-back-main" ref="get_back_main">
            <img class="back-image" ref="back_image" :src="imgURL" @click="router.go(-1)">
            <div class="title">
                <p>消息</p>
            </div>
        </div>

        <div class="message-single" v-for="message in list" @click="router.push(`/chat?id=${message.userId}`)"
            :key=message.userId>
            <img :src="`${MYURL}/public/profiles/${message.info[0].profile}`" alt="">
            <div>
                <p class="name">{{ message.info[0].username }}</p>
                <p class="message">{{ emojiDecode(message.message) }}</p>
            </div>
            <span v-show="message.is_look != 1" class="is_look"></span>
            <p class="time">{{ time(parseInt(message.time))[3] }}</p>
        </div>
    </div>
</template>

<style lang="less" scoped>
.get-back-main {
    position: sticky;
}

.back-image {
    width: 20px;
}

.message-single :first-child {
    margin-top: 15px !important;
}

.message-single {
    position: relative;
    margin-top: 10px;
    padding: 0 0 10px 10px;
    display: flex;

    gap: 15px;

    img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        object-fit: cover;
    }

    div {
        width: 100%;
        border-bottom: .8px solid rgba(0, 0, 0, .1);
        padding-bottom: 15px;
    }

    .name {
        font-weight: 600;
        font-size: 14px;
        width: 80vw;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
        white-space: nowrap;
    }

    .message {
        color: rgba(0, 0, 0, .7);
        font-size: 13px;
        width: 80vw;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
        white-space: nowrap;
    }

    .time {
        position: absolute;
        right: 2%;
        top: 0;
        font-size: 10px;
        color: rgba(0, 0, 0, .7);
    }
}

.is_look {
    position: absolute;
    right: 2%;
    top: 50%;
    transform: translate(0, -50%);
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: var(--bilibili-pink);
}
</style>